﻿using MtApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTEA.State
{

    enum MTOrderInfoType
    {
        None,
        Wave,
        Trend
    }
    class MTOrderInfo
    {
        public MtOrder Order { get; set; }
        public MTOrderInfoType OrderInfoType { get; set; }
        public MTOrderInfo(MtOrder Order)
        {
            this.Order = Order;
            OrderInfoType = MTOrderInfoType.None;
        }
    }
    class MTStateContext
    {
        public static readonly string Tag = "MTStateContext";
        private MtApiClient client;

        private MTOrderInfo orderInfo;

        public MTOrderInfo OrderInfo
        {
            get
            {
                return orderInfo;
            }
        }
        public string Symbol { get; }

        public int TickId
        {
            get
            {
                if (orderInfo==null)
                {
                    return -1;
                }
                return orderInfo.Order.Ticket;
            }
            
        }

        public MTStateContext(string symbol, MtApiClient client)
        {
            Symbol = symbol;
            this.client = client;
            orderInfo = null;
        }

        public void RefreshOrderInfo()
        {
            if(orderInfo!=null)
            {
                Debug.Assert(TickId != -1);
                var refreshOrder = client.GetOrder(TickId, OrderSelectMode.SELECT_BY_TICKET, OrderSelectSource.MODE_TRADES);
                Debug.Assert(refreshOrder != null);
                orderInfo.Order = refreshOrder;
                MTLogcat.d(Tag, "RefreshedOrderInfo");
            }
        }

        #region OrderOperation
        public bool SellOrder(double volume)
        {
            Debug.Assert(TickId == -1);
            Debug.Assert(orderInfo == null);
            int id = client.OrderSendSell(Symbol,volume, 1);
            if (id != -1)
            {
                //卖单成功
                var order = client.GetOrder(id, OrderSelectMode.SELECT_BY_TICKET, OrderSelectSource.MODE_TRADES);
                Debug.Assert(order != null);
                Debug.Assert(order.Operation == TradeOperation.OP_SELL);
                orderInfo = new MTOrderInfo(order);
                return true;
            }
            return false;
        }

        public bool BuyOrder(double volume)
        {
            Debug.Assert(TickId == -1);
            Debug.Assert(orderInfo == null);
            int id = client.OrderSendBuy(Symbol, volume, 1);
            
            if (id != -1)
            {
                //买单成功
                var order=client.GetOrder(id, OrderSelectMode.SELECT_BY_TICKET, OrderSelectSource.MODE_TRADES);
                Debug.Assert(order!=null);
                Debug.Assert(order.Operation==TradeOperation.OP_BUY);
                orderInfo = new MTOrderInfo(order);
                return true;
            }
                
            return false;
        }

        public bool CloseOrder()
        {
            Debug.Assert(TickId != -1);
            Debug.Assert(orderInfo != null);
            var order = client.GetOrder(TickId, OrderSelectMode.SELECT_BY_TICKET, OrderSelectSource.MODE_TRADES);
            Debug.Assert(order!=null);

            var result = client.OrderClose(TickId, order.Lots, 1);
            if (result)
            {
                //Console.WriteLine("OrderClose successfully tickId : " + context.TickId);
                orderInfo = null;
                //experter.setState(new StopState(experter, client));
            }
            return result;
            
        }

        #endregion
    }
}
